<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <title>Welcome</title>
     <!-- 引入jQuery和Bootstrap -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>

    <script>
        $(document).ready(function () {
            // 点击按钮弹出弹框
            $("#addBtn").click(function () {
                $("#dataModal").modal("show");
            });

            // 提交数据到后端
            $("#submitBtn").click(function () {
                const formData = $("#dataForm").serialize(); // 序列化表单数据

                $.ajax({
                    url: "/user",
                    type: "POST",
                    data: formData,
                    success: function (response) {
                        alert("提交成功: " + response);
                        $("#dataModal").modal("hide");
                    },
                    error: function (xhr) {
                        alert("提交失败: " + xhr.statusText);
                    }
                });
            });
        });
    </script>
</head>

<body>
    <h1 th:text="'Hello, ' + ${name} + '!'">Hello, Guest!</h1>
    <!-- 在Thymeleaf模板中（如index.html） -->
    <button id="addBtn" class="btn btn-primary">添加数据</button>

    <!-- 弹框（使用Bootstrap模态框） -->
    <div class="modal fade" id="dataModal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">添加数据</h5>
                </div>
                <div class="modal-body">
                    <form id="dataForm">
                        <input type="text" class="form-control" name="name" placeholder="姓名">
                        <input type="email" class="form-control" name="email" placeholder="年龄">
                    </form>
                </div>
                <div class="modal-footer">
                    <button id="submitBtn" class="btn btn-success">提交</button>
                </div>
            </div>
        </div>
    </div>

   
</body>

</html>